<?php

class GroupDAO {
    public static function getGroups($forSelect = false) {
        $db = Database::getInstance();
        if($forSelect) {
            $db->query("SELECT gid as `id`, name as `text` FROM groups");
            $groups = array();
            $groups[] = array('id'=>0,'text'=>'--- Wybierz grupę ---');
            while($group = $db->getRowAssoc()) {
                $groups[] = $group;
            }
            return $groups;
        } else {
            $db->query("SELECT * FROM groups");
            if($db->getNumRows() > 0) {
                return $db->getObjectArray();
            } else {
                return array();
            }
        }

    }
    public static function getGroupInfo($gid) {
        $db = Database::getInstance();
        $db->query("SELECT * FROM groups WHERE gid=" . $gid);
        if($db->getNumRows() < 1) {
            return false;
        }
        return $db->getRowAssoc();
    }
    public static function groupExist($group) {
        $db = Database::getInstance();
        if($group->getGid() > 0) {
            $db->query("SELECT * FROM groups WHERE gid=" . $group->getGid());
            if($db->getNumRows() > 0) {
                return true;
            } else {
                return false;
            }
        } else {
            $db->query("SELECT * FROM groups WHERE name LIKE '" . $group->name . "'");
            if($db->getNumRows() > 0) {
                return true;
            } else {
                return false;
            }
        }
    }
    public static function createGroup($group) {
        $db = Database::getInstance();
        $db->query("INSERT INTO groups(name) VALUES('" . $group->name . "')");
        return $db->insertId();
    }
    
    public static function deleteGroup($group) {
        $db = Database::getInstance();
        $db->query("UPDATE users SET gid=0 WHERE gid=" . $group->getGid());
        $db->query("DELETE FROM groups WHERE gid=" . $group->getGid());
        return (bool)$db->getAffectedRows();
    }
    
    public static function updateGroup($group) {
        $db = Database::getInstance();
        $db->query("UPDATE groups SET name='" . $group->name . "' WHERE gid=" . $group->getGid());
        return $db->getAffectedRows() > 0;
    }
    public static function getUsers($group) {
        $db = Database::getInstance();
        $db->query("SELECT uid FROM users WHERE gid=" . $group->getGid());
        $users = array();
        $uids = array();
        
        while($uid = $db->getOne()) {
            $uids[] = $uid;
        }
        foreach($uids as $key => $uid) {
            $users[] = new AnyUser((int)$uid);
        }
        return $users;
    }
    public static function addUser($group, $user) {
        $db = Database::getInstance();
        $db->query("UPDATE users SET gid=" . $group->getGid() . " WHERE uid=" . $user->getUid());
        return (bool)$db->getAffectedRows();
    }
    
    public static function getRights($gid) {
        $db = Database::getInstance();
        $db->query("
                    SELECT
                        g.mid, g.lvl
                    FROM
                        group_rights g
                    WHERE
                        g.gid = " . (int)$gid);

        $gRights = $db->getObjectArray();
        $rights = array();
        if(count($gRights)) {
            foreach($gRights as $key => $right) {
                $rights[$right->mid] = $right->lvl;
            }
        }
        return $rights;
    }
}
?>
